package com.ipartek.formacion.miprimerwebapp.sevlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ipartek.formacion.primeraWebApp.bbdd.AlumnoDAO;
import com.ipartek.formacion.primeraWebApp.bbdd.DAOFactory;
import com.ipartek.primeraWebApp.beans.Alumno;

/**
 * Servlet implementation class AlumnosServlet
 */
public class AlumnosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private static final String view_List_alumnos  = "alumnos.jsp";
    private RequestDispatcher dispatcher = null;
	/**
     * @see HttpServlet#HttpServlet()
     */
    public AlumnosServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//obtener el dispatcher
		dispatcher = request.getRequestDispatcher(view_List_alumnos);
		
		DAOFactory factoriaODBC = DAOFactory.getDAOFactory(1);
		AlumnoDAO alumno = factoriaODBC.getAlumno();
		
		try
		{
			List<Alumno> listaAlumnos = alumno.getAlumnos();
			//Pasar parametro a alumnos.jsp
			request.setAttribute("listaAlumnos", listaAlumnos.subList(0, 10));
		}
		catch(Exception e)
		{
			
		}
		
		//hacemos el forward
		dispatcher.forward(request, response);
	}

}
